<template>
	<view class="page">
		<z-paging ref="paging" v-model="dataList" @query="queryList">
			<template #top>
				<u-navbar title="我的订单" safeAreaInsetTop fixed placeholder :isBack="false"
					:border-bottom="false"></u-navbar>
				<view style="padding: 24rpx;background: #fff;border-bottom: solid 1px #f2f2f2;">
					<u-tabs :list="tabList" :current="tabIndex" :bar-style="{background: '#0BB1EF'}" bar-width="48"
						active-color="#0BB1EF" inactive-color="#999999" @change="changeTab"></u-tabs>
				</view>
			</template>

			<!-- 列表 -->
			<view style="background: #fff;">
				<view style="padding: 24rpx 32rpx 0 32rpx;margin-bottom: 20rpx;" v-for="(item, index) in dataList"
					:key="index" @click="orderDetail()">
					<view style="border-bottom: #f2f2f2 solid 1px;padding-bottom: 24rpx;">

						<view style="display: flex;align-items: center;justify-content: space-between;">
							<view style="color: #ff9900;font-weight: bold;font-size: 32rpx;">
								￥{{item.orderData.total_price}}</view>
							<view style="color: #ff9900;" v-if="tabIndex == 0">等待接单</view>
							<view style="color: #ff9900;" v-if="tabIndex == 1">待服务</view>
							<view style="color: #ff9900;" v-if="tabIndex == 2">服务中</view>
							<view style="color: #ff9900;" v-if="tabIndex == 3">已完成</view>
						</view>
						<view style="margin-top: 16rpx;">
							<view style="display: flex;align-items: center;">
								<view style=";background: #0BB1EF;border-radius: 20rpx;color: white;
								padding: 12rpx 18rpx;font-size: 24rpx;">服</view>
								<view style="flex: 1;margin-left: 40rpx;">
									<view style="font-weight: bold;font-size: 30rpx;">{{item.gigjobs_name}}</view>
									<!-- <view style="color: #999;margin-top: 10rpx;">
										<text v-for="(it, ind) in item.masterData" :key="ind" 
										style="padding-right: 24rpx;font-size: 26rpx;">{{it.master_name}}</text>
									</view> -->
								</view>
							</view>
							<view style="margin-top: 16rpx;display: flex;align-items: center;">
								<view style=";background: #0daff0;border-radius: 20rpx;color: #fff;
								padding: 12rpx 18rpx;font-size: 24rpx;">目</view>
								<view style="flex: 1;margin-left: 40rpx;">
									<view style="font-weight: bold;font-size: 30rpx;
									width: 300px;
									overflow: hidden;
									text-overflow: ellipsis;
									white-space: nowrap;">{{item.orderData.location_detail}}</view>
									<view style="color: #999;font-size: 26rpx;margin-top: 10rpx;">
										<text>{{item.orderData.nickname}}</text>
										<text style="padding-left: 24rpx;">{{item.orderData.mobile}}</text>
									</view>
								</view>
							</view>
						</view>
						<view
							style="font-size: 28rpx;margin-top: 16rpx;display: flex;align-items: center;justify-content: space-between;">
							<view style="display: flex;align-items: center;width: 100%;" class="button-group">
								<view v-if="tabIndex == 0 || item.status == 0" style="display: flex;align-items: center;width: 100%;">
									<view class="hollowcss" @click="clickRefuse(item)">拒单</view>
									<view class="solidcss" @click="clickReceiving(item)">接单</view>
								</view>
								<!-- v-if="item.status == 1 && item.service_status == 0" -->
								<view v-if="tabIndex == 1"
									style="display: flex;align-items: center;width: 100%;">
									<view class="hollowcss" @click="callPhone(item.orderData.mobile)">联系雇主</view>
									<!-- v-if="item.arrive_status == 0" -->
									<view class="solidcss" v-if="index == 0"  @click="clickReach(item)">
										点击到达目的地</view>
										<!-- v-if="item.arrive_status == 1" -->
									<view class="solidcss" v-if="index == 1"  @click="startService(item)">
										开始服务</view>
								</view>
								<!-- v-if="item.service_status == 1 && item.finish_status == 0" -->

								<view  v-if="tabIndex == 2"
									style="display: flex;align-items: center;width: 100%;">
									<view class="hollowcss" @click="callPhone(item.orderData.mobile)">联系雇主</view>
									<view class="solidcss" @click="endService(item)">完成服务</view>
								</view>
								<view  v-if="tabIndex == 3"
									style="display: flex;align-items: center; justify-content: flex-end; width: 100%;">
									<view class="hollowcss" @click="callPhone(item.orderData.mobile)">删除订单</view>
								
								</view>
							</view>
						</view>

					</view>
				</view>
			</view>

			<template #bottom>
				<view class="footer">
					<button class="confirm-btn" @click="confirmSelection"
						style="background: #1877f2;color: #fff;">确定</button>
				</view>
			</template>
		</z-paging>

		<u-toast ref="uToast" />

		<view style="height: 200rpx;"></view>

		<!-- <view style="position: fixed;right: 32rpx;bottom: 204rpx;" @click="jumpTo('/pages/prescription/ordinaryEdit')">
			<image src="@/static/img/add.png" mode="widthFix" style="width: 80rpx;"></image>
		</view> -->
		<!-- 更多操作 -->
		<u-action-sheet :list="moreList" v-model="moreShow" border-radius="32" @click="moreClick"></u-action-sheet>

		<!-- 服务报价 -->
		<u-popup v-model="quotationShow" mode="bottom" border-radius="16" :mask-close-able="false">
			<view style="border-radius: 24rpx;background: #fff;padding: 24rpx;">
				<view style="margin-top: 12rpx;">
					<view class="cu-bar bg-white">
						<view style="text-align: center;font-weight: bold;font-size: 30rpx;">
							拒绝理由
						</view>
					</view>
					<view>
						<view style="padding:  32rpx;">
							<view
								style="padding: 24rpx;background: #fff;border-radius: 24rpx;font-size: 28rpx;color: #333A47;">
								<view style="padding-bottom: 16rpx;width: 120rpx;">理由</view>
								<view style="padding: 24rpx;background-color: #f6f6f6;border-radius: 12rpx;">
									<input type="digit" placeholder="请输入拒绝理由" v-model="quotation">
								</view>
								<view style="color: #ff9900;font-size: 24rpx;margin-top: 16rpx;">* 请认真填写拒绝理由</view>
							</view>
						</view>
					</view>
					<view style="height: 8rpx;width: 100%;background: #F5F8FF;"></view>
					<view style="display: flex;align-items: center;">
						<view style="padding: 32rpx 0;text-align: center;color: #333A47;font-size: 32rpx;width: 50%;"
							@click="quotationShow = false"> 取消 </view>
						<view style="padding: 48rpx 32rpx;text-align: center;color: #0daff0;font-size: 32rpx;flex: 1;">
							<view style="font-weight: bold;" @click="quotationConfirm">确认</view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>

		<!-- 开始服务 -->
		<u-popup v-model="endShow" mode="bottom" border-radius="16" :mask-close-able="false">
			<view style="border-radius: 24rpx;background: #fff;padding: 24rpx;">
				<view style="margin-top: 12rpx;">
					<view class="cu-bar bg-white">
						<view style="text-align: center;font-weight: bold;font-size: 30rpx;">
							结束服务
						</view>
					</view>
					<view>
						<view style="padding:  32rpx;">
							<view
								style="padding: 24rpx;background: #fff;border-radius: 24rpx;font-size: 28rpx;color: #333A47;">
								<view style="padding-bottom: 16rpx;width: 120rpx;">服务码</view>
								<view style="padding: 24rpx;background-color: #f6f6f6;border-radius: 12rpx;">
									<!-- <input type="digit" placeholder="请输入拒绝理由" v-model="quotation"> -->
									<u-message-input :maxlength="5" @finish="endfinish" :focus="true"></u-message-input>
								</view>
								<view style="color: #ff9900;font-size: 24rpx;margin-top: 16rpx;">* 请问下单客户服务码</view>
							</view>
						</view>
					</view>
					<view style="height: 8rpx;width: 100%;background: #F5F8FF;"></view>
					<view style="display: flex;align-items: center;">
						<view style="padding: 32rpx 0;text-align: center;color: #333A47;font-size: 32rpx;width: 50%;"
							@click="endShow = false"> 取消 </view>
						<view style="padding: 48rpx 32rpx;text-align: center;color: #0daff0;font-size: 32rpx;flex: 1;">
							<view style="font-weight: bold;" @click="endConfirm">确认</view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>

		<!-- 删除弹窗 -->
		<u-modal v-model="delShow" title="确定删除" :show-cancel-button="true" confirm-text="确认删除"
			:title-style="{fontWeight: 'bold'}" @confirm="lvDelYes">
			<view class="slot-content">
				<view style="padding: 48rpx 32rpx 64rpx 32rpx;">
					<view style="font-size: 28rpx;color: #101010;text-align: center;">是否确定删除改等级？删除后不可恢复</view>
				</view>
			</view>
		</u-modal>

		<u-tabbar v-model="current" :list="tabbar" active-color="#0daff0"></u-tabbar>
	</view>
</template>

<script>
	import {
		getTabbarConfig
	} from '@/common/tabbar';
	import {
		request
	} from 'utils/request.js';
	export default {
		data() {
			return {
				current: 1,
				delShow: false,
				moreShow: false,
				moreList: [],
				tabbar: getTabbarConfig(true),

				operate_id: 0,
				quotationShow: false,
				quotation: '',

				endShow: false,
				com_code: '',

				dataList: [{
					orderData: {
						total_price: 3.25,
						nickname: '王先生',
						mobile: '156****5412',
						location_detail: '贵阳市南明区花果园金融街1号1824'
					},
					gigjobs_name: '管道疏通(此处为需求工种)',
				}, {
					orderData: {
						total_price: 3.25,
						nickname: '王先生',
						mobile: '156****5412',
						location_detail: '贵阳市南明区花果园金融街1号1824'
					},
					gigjobs_name: '管道疏通(此处为需求工种)',
				}],
				keyword: '',

				tabIndex: 0,
				tabList: [{
						name: '待接单',
						filter: 'isnotb'
					},
					{
						name: '待服务',
						filter: 'amount'
					},
					// {name: '等待派单', filter: 'isnotm'},
					{
						name: '服务中',
						filter: 'service'
					},
					{
						name: '已完成',
						filter: 'completed'
					},
					{
						name: '退款/售后',
						filter: 'maintanance'
					},
				],
				serviceItemsSum: 0,

				// 示例数据
				listData: [],
				selectedIndexes: [], // 存储被选中的索引
				selectedTags: [],

			};
		},
		onLoad(options) {
			// 从父页面传递的已选标签
			if (options.selected) {
				this.selectedTags = JSON.parse(options.selected);
			}
			// this.filteredTags = [...this.allTags];
			// this.loadSelectedTags();
			uni.$on('htmlRefresh', (e) => {
				this.$refs.paging.reload() //重置
				return;
				let selectedTagsJson = e.selectedTagsJson;
				// console.log('子页面---->>>>>>', selectedTagsJson);
				if (selectedTagsJson) {
					// this.selectedTags = JSON.parse(selectedTagsJson);
					// console.log('selectedTags: ', this.selectedTags)
					// this.serviceItemsSum = this.selectedTags.length;
				}
			});
		},
		methods: {
			orderDetail(){
				uni.navigateTo({
					url:'/pages/order/detail'
				})
			},
			// 接单
			clickReceiving(item) {
				request.postWithToken({
					url: '/order/postReceivingOrder',
					data: item
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none'
					});
					this.$refs.paging.reload() //重置
				})
			},
			// 联系雇主
			callPhone(mobile) {
				uni.makePhoneCall({
					phoneNumber: mobile
				})
			},
			// 点击到达
			clickReach(item) {
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						console.log(res)
						// this.latitude = res.latitude;
						// this.longitude = res.longitude;
						this.reachSubm(item, res.latitude, res.longitude);
					},
					fail: (err) => {
						console.log('获取位置失败:', err);
						uni.showToast({
							title: '获取位置失败，使用默认位置',
							icon: 'none'
						});
					}
				});

			},
			reachSubm(item, latitude, longitude) {
				let params = {};
				params = item;
				params.latitude = latitude;
				params.longitude = longitude;
				request.postWithToken({
					url: '/order/postReachOrder',
					data: params
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none'
					});
					if (res.code == 1) {
						this.$refs.paging.reload() //重置
					}
				})
			},
			// 开始服务
			startService(item) {
				request.postWithToken({
					url: '/order/postStartServiceOrder',
					data: item
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none'
					});
					if (res.code == 1) {
						this.$refs.paging.reload() //重置
					}
				})
			},
			// 结束服务
			endService(item) {
				this.operate_id = item.id
				this.endShow = true;
			},
			endfinish(item) {
				console.log('输入结束，当前值为：' + item);
				this.com_code = item;
			},
			endConfirm(e) {
				// console.log('endConfirm： ', e)
				request.postWithToken({
					url: '/order/postEndServiceOrder',
					data: {
						operate_id: this.operate_id,
						com_code: this.com_code
					}
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none'
					});
					if (res.code == 1) {
						this.endShow = false;
						this.$refs.paging.reload() //重置
					}
				})
			},

			changeTab(index) {
				console.log('tabIndex： ', index)
				this.tabIndex = index;
				this.$refs.paging.reload() //重置
			},
			// 搜索标签
			onSearch() {
				this.$refs.paging.reload() //重置
			},
			// 拒单
			clickRefuse(item) {
				this.operate_id = item.id
				this.quotationShow = true;
			},
			quotationConfirm() {
				console.log('quotation: ', this.quotation);
				if (this.quotation == '') {
					uni.showToast({
						title: '拒绝理由不能为空',
						duration: 2000,
						icon: 'none'
					});
					return;
				}
				request.postWithToken({
					url: '/order/postRefuseOrder',
					data: {
						operate_id: this.operate_id,
						quotation: this.quotation
					}
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none'
					});
					if (res.code == 1) {
						this.$refs.paging.reload() //重置
						this.quotationShow = false;
					}
				})
			},
			// 确认选择
			confirmSelection() {
				// 保存到本地存储
				// uni.setStorageSync('selectedTags', JSON.stringify(this.selectedTags));
				let selectedTagsJson = JSON.stringify(this.selectedTags);
				// 返回上一页
				setTimeout(() => {
					uni.navigateBack();
					uni.$emit('htmlRefresh', {
						selectedTagsJson: selectedTagsJson
					});
				}, 600);
				// uni.navigateBack();
			},
			moreClick(e) {
				console.log(e)
			},
			// 获取列表
			queryList(pageNo, pageSize) {

				let params = {};
				params.page = pageNo;
				params.tabIndex = this.tabIndex;
				params.keyword = this.keyword;
				request.getWithToken({
					url: '/order/orderLis',
					data: params
				}).then(res => {
					// console.log(res)
					this.$refs.paging.complete([{
						orderData: {
							total_price: 3.25,
							nickname: '王先生',
							mobile: '156****5412',
							location_detail: '贵阳市南明区花果园金融街1号1824'
						},
						gigjobs_name: '管道疏通(此处为需求工种)',
					}, {
						orderData: {
							total_price: 3.25,
							nickname: '王先生',
							mobile: '156****5412',
							location_detail: '贵阳市南明区花果园金融街1号1824'
						},
						gigjobs_name: '管道疏通(此处为需求工种)',
					}]);
					// this.filteredTags = [...res.data];
					// this.$refs.paging.complete(this.filteredTags);
				}).catch(err=>{
					console.log('结束')
					this.$refs.paging.complete([{
						orderData: {
							total_price: 3.25,
							nickname: '王先生',
							mobile: '156****5412',
							location_detail: '贵阳市南明区花果园金融街1号1824'
						},
						gigjobs_name: '管道疏通(此处为需求工种)',
					}, {
						orderData: {
							total_price: 3.25,
							nickname: '王先生',
							mobile: '156****5412',
							location_detail: '贵阳市南明区花果园金融街1号1824'
						},
						gigjobs_name: '管道疏通(此处为需求工种)',
					}]);
					
				})
			},
			followClick(e) {
				let talkData = uni.getStorageSync('talkData');

			},
			jumpTo(path) {
				uni.navigateTo({
					url: path
				})
			},
			onSubmitSave() {
				uni.navigateTo({
					url: '/pages/prescription/selectMedicine'
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #f2f2f2 !important;
	}
</style>
<style lang="scss" scoped>
	.button-group {
		margin: 10rpx 0;
		display: flex;
		justify-content: flex-end;
		margin-top: 30rpx;
	}

	.solidcss {
		color: #fff;
		border: 2rpx solid #0daff0;
		background: #0daff0;
		border-radius: 10rpx;
		padding: 16rpx 0rpx;
		// width: 80px;
		flex: 1;
		text-align: center;
		margin-left: 24rpx;
	}

	.hollowcss {
		color: #909399;
		border: 2rpx solid #909399;
		border-radius: 10rpx;
		padding: 16rpx 0rpx;
		width: 120px;
		text-align: center;
		margin-left: 24rpx;
	}
</style>